package pt.fct.mpc.lastfm.generic;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import pt.fct.mpc.lastfm.R;
import pt.fct.mpc.lastfm.connectivity.LastfmBluetoothUnavailableException;
import pt.fct.mpc.lastfm.connectivity.LastfmURL;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

public class ImageActivity extends Activity {
	
	private static final String TAG = "Last.fm Image Activity";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image);
        
		//Reads the info sent by the intent
		Intent the_intent_that_gave_life_to_everything = getIntent();
		Bundle intent_data = 
			the_intent_that_gave_life_to_everything.getExtras();

		if (intent_data == null)
		{
			Log.e(TAG, "No extra data");
			finish();
		}

		String imageUrl = intent_data.getString("url");
		if (imageUrl == null)
		{
			Log.e(TAG, "Null bundle parameters: artist name = "
					+ imageUrl);
			finish();
		}

        ImageView imgV = 
        	(ImageView) findViewById(R.id.image);
        imgV.setImageBitmap(urlToImage(imageUrl));
	}

	static public Bitmap urlToImage(String imageUrl) {

		LastfmURL myFileUrl = null;
		try {
			myFileUrl= new LastfmURL(imageUrl);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

		InputStream is = null;
		try {
			is = myFileUrl.openStream();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (LastfmBluetoothUnavailableException e) {
			e.printStackTrace();
		}

		return BitmapFactory.decodeStream(is);
	}
}